package com.itheima.health.controller.backend;

import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;

import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 体检检查项管理
 */
@RestController
@RequestMapping("/checkitem")
@Tag(name = "CheckItemController")
public class CheckItemController {
    @Autowired
    private CheckItemService checkItemService;


    /**
     * 添加检查项
     *
     * @param checkItem 前端提交过来的检查项数据
     * @return 统一响应结果
     */

    @PostMapping(value = "/add.do")
    @Operation(description = "add")

    public Result add(@RequestBody CheckItem checkItem, HttpServletRequest request) {

        checkItemService.add(checkItem);
        String requestURI = request.getRequestURI();
        System.out.println("requestURI = " + requestURI);

        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }


    @PostMapping(value = "/findPage.do")
    @Operation(description = "findPage")

    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = checkItemService.findPage(queryPageBean);

        return pageResult;
    }


    @GetMapping(value = "/delete.do")
    @Operation(description = "delete")

    public Result delete(@Param("id") Integer id) {
        Boolean delete = checkItemService.delete(id);
        String message = delete ? MessageConstant.DELETE_CHECKGROUP_SUCCESS : MessageConstant.DELETE_CHECKGROUP_FAIL;
        return new Result(delete, message);
    }

    /**
     * 根据id查询检查项
     *
     * @param id 检查项id
     * @return 统一响应结果, 包含检查项数据
     */
    @GetMapping("/findById.do")
    @Operation(description = "findById")

    public Result findById(@Param("id") Integer id) {
        // 1.调用业务层根据id查询检查项
        CheckItem checkItem = checkItemService.findById(id);
        // 2.响应统一结果Result,包含检查项数据
//        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
        return Result.ok(MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
    }

    /**
     * 编辑检查项
     *
     * @param checkItem 检查项数据
     * @return 统一响应结果
     */
    @PostMapping("/edit.do")
    @Operation(description = "edit")
    public Result edit(@RequestBody CheckItem checkItem) {
        // 1.调用业务层编辑检查项
        checkItemService.edit(checkItem);
        // 2.响应统一结果Result
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    /**
     * 查询所有检查项
     *
     * @return 所有检查项数据
     */
    @Operation(description = "查询所有检查项")
    @GetMapping("/findAll.do")
    public Result findAll() {

        List<CheckItem> checkItems = checkItemService.findAll();

        return Result.ok(MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItems);
    }
}
